package com.lsfb.light.View;

import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;

import com.lsfb.light.R;
import com.zgscwjm.lsfbbasetemplate.LsfbAppManager;
import com.zgscwjm.lsfbbasetemplate.config.Config;
import com.zgscwjm.lsfbbasetemplate.utils.LsfbLog;
import com.zgscwjm.lsfbbasetemplate.view.bottombar.LsfbBottomBar;

import java.util.List;

/**
 * Created by Administrator on 2018/4/23 0023.
 */

public abstract class MyFragmentActivity extends FragmentActivity implements View.OnClickListener {


    public FragmentPagerAdapter mAdapter;

    /**
     * 全局LayoutInflater对象,以实例化
     */
    private LayoutInflater mInflater;

    /**
     * viewpage 是否可以滚动
     */
    private boolean isScroll = true;

    public LinearLayout getLsfb_base() {
        return lsfb_base;
    }

    /**
     * 主布局
     */
    private LinearLayout lsfb_base = null;


    /**
     * 内容布局
     */
    public ViewPager viewPager;


    /**
     * 默认的buttonbar
     */
    private LsfbBottomBar lsfb_buttonBar = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        LsfbLog.e(this.getClass(), "---------------onCreate begin");


        requestWindowFeature(Window.FEATURE_NO_TITLE);

        // 设置竖屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        // 实例化LayoutInflater
        mInflater = LayoutInflater.from(this);

        lsfb_base = new LinearLayout(this);

        lsfb_base.setClipChildren(false);

        lsfb_base.setOrientation(LinearLayout.VERTICAL);
        setMainBackground();

        // 开启沉浸式菜单栏
        if (Config.Style_Immersion) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            lsfb_base.setFitsSystemWindows(true);
            lsfb_base.setClipToPadding(true);
        }


        viewPager = new ViewPager(this) {


            @Override
            public boolean onTouchEvent(MotionEvent arg0) {
                if (isScroll) {
                    return super.onTouchEvent(arg0);
                } else {
                    return false;
                }
            }


            @Override
            public boolean onInterceptTouchEvent(MotionEvent ev) {
                if (isScroll) {
                    return super.onInterceptTouchEvent(ev);
                } else {
                    return false;
                }

            }

        };


        viewPager.setOffscreenPageLimit(4);
        viewPager.setId(View.generateViewId());

        lsfb_buttonBar = new LsfbBottomBar(this, this);

        LinearLayout.LayoutParams viewpage_ll = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, 1);

//        viewpage_ll.setMargins(0, 0, 0, DensityUtils.dp2px(this, 3));


        lsfb_base.addView(viewPager, viewpage_ll);
        setContentView(lsfb_base, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

//        mViewPage = new ViewPager(this);
//        viewPager.addView((View) mViewPage, WindowManager.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);


        lsfb_base.addView(lsfb_buttonBar);


        initView();
        initEvent();
        initData();


        LsfbAppManager.getAppManager().addActivity(this);
        LsfbLog.e(this.getClass(), "---------------onCreate end");


    }


    @Override
    protected void onStart() {
        super.onStart();
        LsfbLog.e(this.getClass(), "---------onStart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        LsfbLog.e(this.getClass(), "---------onResume ");
    }

    @Override
    protected void onStop() {
        super.onStop();
        LsfbLog.e(this.getClass(), "---------onStop ");
    }

    @Override
    protected void onPause() {
        super.onPause();
        LsfbLog.e(this.getClass(), "---------onPause ");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        LsfbLog.e(this.getClass(), "---------onRestart ");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        LsfbLog.e(this.getClass(), "---------onDestroy ");
        LsfbAppManager.getAppManager().finishActivity(this);
    }


    /**
     * 设置背景色
     *
     * @author zgscwjm
     * @time 2015年12月8日-下午4:03:47
     */
    protected void setMainBackground(int color) {
        Config.Style_AppColor = color;
        setMainBackground();
    }

    /**
     * 设置背景色
     *
     * @author zgscwjm
     * @time 2015年12月8日-下午4:03:47
     */
    protected void setMainBackground() {
        this.lsfb_base.setBackgroundColor(getResources().getColor(R.color.white_color));
//        this.lsfb_base.setBackgroundColor(-49023);
    }


    /**
     * 得到bottombar
     *
     * @return
     * @author zgscwjm
     * @time 2015年12月22日-下午6:13:15
     */
    public LsfbBottomBar getButtonBar() {
        return lsfb_buttonBar;
    }


    //设置fragment
    public void setTabFragments(final List<Fragment> mFragments) {
        mAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {


            @Override
            public int getCount() {
                return mFragments.size();
            }

            @Override
            public Fragment getItem(int position) {

                return mFragments.get(position);
            }
        };


        viewPager.setAdapter(mAdapter);

    }

    public abstract void initView();

    public abstract void initEvent();

    public abstract void initData();

    public abstract void tabOnClick(View view, ViewPager viewPager);

    public void onClick(View view) {
        tabOnClick(view, viewPager);
    }

    ;

    public boolean isScroll() {
        return isScroll;
    }

    /**
     * viewpage 是否可以滑动切换
     *
     * @param isScroll
     */
    public void setIsScroll(boolean isScroll) {
        this.isScroll = isScroll;
    }

//    public void onEventDef(DefEvent defEvent)
//    {
//        switch (defEvent.getCode())
//        {
//            case DefEvent.DEFEVENT_NET_ERROE:
//                T.showShort(this, "网络错误");
//                break;
//        }
//    }

}
